<?php
class myPDO{
    private static $pdo;
    public $host="120.79.218.85";
    public $port="3306";
    public $dbname ="login";
    public $charset="UTF8";
    public $user="root";
    public $password="Wst5201314.";
    public function __construct(){
        $this->getInstance();
        //code
    }
    private function __clone(){
        //code
    }
    /**
     * 获取实例化的PDO，单例模式
     * @return PDO
     */
    private function getInstance(){
        if(!(self::$pdo instanceof PDO)){
            $dsn ="mysql:host=".$this->host.";port=".$this->port.";dbname=".$this->dbname.";charset=".$this->charset;
            try {
                self::$pdo = new PDO($dsn,$this->user, $this->password, array(PDO::ATTR_PERSISTENT => true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));                      //保持长连接
                self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            } catch (PDOException $e) {
                print "Error:".$e->getMessage()."<br/>";
                die();
            }
        }
        return self::$pdo;
    }

    public function getDate($sql){
        $pdo = self::$pdo;
        $rs = $pdo->query($sql);
//        $data=$rs->fetchAll();//取出所有结果
        $data=$rs->fetch();//取出所有结果
        return $data;
    }
    public function update($sql){
        $pdo = self::$pdo;
        return $pdo->exec($sql);
    }
}
